package com.xhh.greynewsapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.xhh.greynewsapp.MainActivity;
import com.xhh.greynewsapp.R;
import com.xhh.greynewsapp.model.user.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-27 13:42
 **/
public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.et_login_username)
    EditText etUsername;
    @BindView(R.id.et_login_pwd)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_to_register)
    Button btnToRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);


        btnLogin.setOnClickListener(click->{
            // 登录
            String loginUsername = etUsername.getText().toString().trim();
            String loginName = etPassword.getText().toString().trim();

            // TODO 先使用Bmob,后期在自己搞
            User user = new User();
            user.setUsername(loginUsername);
            user.setPassword(loginName);

            // Bmob用户登录
            user.login(new SaveListener<User>() {
                @Override
                public void done(User user, BmobException e) {
                    if(e == null){
                        // 登录成功
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    } else {
                        Log.e("LoginActivity", "失败" + e);
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                    }
                }
            });
        });

        btnToRegister.setOnClickListener(click->{
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            finish();
        });

    }
}
